<template>
	<view>
		<cu-custom :isBack="true" bgColor="bg-theme text-white">
			<block slot="backText">返回</block>
			<block slot="content" >申请直播</block>
		</cu-custom>
		<form @submit="submit" >
			<view class="cu-form-group">
				<view class="title">直播间名字</view>
				<input class="text-right" placeholder="请输入名字" name="name"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">直播间标题</view>
				<input class="text-right" placeholder="请输入标题" name="title"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">直播类型</view>
				<picker @change="liveChange" :value="index" :range="category" range-key="name">
					<view class="picker">
						{{index>-1?category[index].name:'选择'}}
					</view>
				</picker>
				
			</view>
			
			<view class="cu-bar bg-white margin-top">
				<view class="action">
					直播封面
				</view>
				<view class="action">
					{{imgList.length}}/1
				</view>
			</view>
			<view class="cu-form-group">
				<view class="grid col-2 grid-square flex-sub">
					<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
					 <image :src="imgList[index]" mode="aspectFill"></image>
						<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
							<text class='cuIcon-close'></text>
						</view>
					</view>
					<view class="solids" @tap="ChooseImage" v-if="imgList.length<1">
						<text class='cuIcon-cameraadd'></text>
					</view>
				</view>
				<input type="text" name="posterimage" style="display: none;" v-model="posterimage">
			</view>
			
			<view class="padding">
				<button form-type="submit" class="cu-btn block lg bg-theme">开通直播</button>
			</view>
			
		</form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				category:[],
				index:-1,
				imgList: [],
				posterimage:false,
			}
		},
		onLoad() {
			this.getCategory()
		},
		methods: {
			submit(e){
				console.log(e)
				var data = e.detail.value
				if(this.index==-1){
					return this.$api.msg('请选择直播分类')
				}
				data['live_category_id'] = this.category[this.index].id;
				this.util.csAjax('/addons/live/api.room/add',data,(ret)=>{
					console.log(ret)
					this.$api.msg(ret.msg)
					this.$fire.fire('update')
					uni.navigateBack({})
				})
				
			},
			liveChange(e) {
				this.index = e.detail.value
			},
			ChooseImage() {
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						if (this.imgList.length != 0) {
							this.imgList = this.imgList.concat(res.tempFilePaths)
						} else {
							this.imgList = res.tempFilePaths
						}
						this.util.upload(res.tempFilePaths[0],(ret)=>{
							console.log(ret)
							var result = JSON.parse(ret)
							if(result.data.url){
								this.posterimage = result.data.url
							}
							
						},'上传中');
					}
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '召唤师',
					content: '确定要删除这段回忆吗？',
					cancelText: '再看看',
					confirmText: '再见',
					success: res => {
						if (res.confirm) {
							this.imgList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},
			getCategory(){
				this.util.csAjax('/addons/live/api.index/category',{},(ret)=>{
					this.category = ret.data
				})
			}
		}
	}
</script>

<style>

</style>
